import Vue from "vue";
import Router from "vue-router";
import store from "@/store";

Vue.use(Router);

const router = new Router({
  mode: "history",
  base: "/",
  routes: [
    // 首页
    {
      path: "/",
      name: "homePage",
      component: () => import("@/views/homePage"),
    },
    // 理论做题页
    {
      path: "/questionbank",
      name: "questionBank",
      component: () => import("@/views/questionBank"),
    },
    // 继续答题页
    {
      path: "/questionbank/continue",
      name: "continue",
      component: () => import("@/views/questionBank/continue"),
    },
    // 重新答题页
    {
      path: "/questionbank/again",
      name: "again",
      component: () => import("@/views/questionBank/again"),
    },
    // 理论做题完成页
    {
      path: "/questionbank/completed",
      name: "completed",
      component: () => import("@/views/questionBank/completed"),
    },
    //案例分析题
    {
      path: "/questionbank/case",
      name: "case",
      component: () => import("@/views/questionBank/case"),
    },
    // 登录页
    {
      path: "/login",
      name: "loginPage",
      component: () => import("@/views/loginPage"),
    },
    // 登录页1
    {
      path: "/login1",
      name: "loginPage1",
      component: () => import("@/views/loginPage1"),
    },
    // 忘记密码
    {
      path: "/forgotpassword",
      name: "forgotPassword",
      component: () => import("@/views/forgotPassword"),
    },
    // 赛项资源页
    {
      path: "/eventresources",
      name: "eventResources",
      component: () => import("@/views/eventResources"),
    },
    // 赛项资源详情页
    {
      path: "/resourcesdetails",
      name: "resourcesDetails",
      component: () => import("@/views/resourcesDetails"),
    },
    // 搜索页
    {
      path: "/search",
      name: "searchPage",
      component: () => import("@/views/searchPage"),
    },
    // 个人中心
    {
      path: "/personalcenter",
      name: "personalCenter",
      component: () => import("@/views/personalCenter"),
    },
    //添加班级
    {
      path: "/saveclass",
      name: "saveClass",
      component: () => import("@/views/personalCenter/saveClass"),
    },
    //班级详情
    {
      path: "/classdetails",
      name: "classDetails",
      component: () => import("@/views/personalCenter/classDetails"),
    },
    //个人练习记录
    {
      path: "/studentrecord",
      name: "studentRecord",
      component: () => import("@/views/personalCenter/studentRecord"),
    },
    //老师练习记录
    {
      path: "/teacherrecord",
      name: "teacherRecord",
      component: () => import("@/views/personalCenter/teacherRecord"),
    },
    //学习统计时长
    {
      path: "/timecount",
      name: "timeCount",
      component: () => import("@/views/personalCenter/timeCount"),
    },
    //学习详情
    {
      path: "/learningdetails",
      name: "learningDetails",
      component: () => import("@/views/personalCenter/learningDetails"),
    },
    //行业资源
    {
      path: "/industryresources",
      name: "industryResources",
      component: () => import("@/views/industryResources"),
    },
    //行业资源详情页
    {
      path: "/industry",
      name: "industry",
      component: () => import("@/views/industry"),
    },

    // 1+x证书资源页
    {
      path: "/bookresources",
      name: "bookResources",
      component: () => import("@/views/bookResources"),
    },
    //1+x证书详情页
    {
      path: "/onexbook",
      name: "oneXbook",
      component: () => import("@/views/oneXbook"),
    },
    //技能培训资源页
    {
      path: "/skillsresources",
      name: "skillsResources",
      component: () => import("@/views/skillsResources"),
    },
    //技能培训详情页
    {
      path: "/skills",
      name: "skills",
      component: () => import("@/views/skills"),
    },
    //课程资源资源页
    {
      path: "/courseresources",
      name: "courseResources",
      component: () => import("@/views/courseResources"),
    },
    //课程资源详情页
    {
      path: "/course",
      name: "course",
      component: () => import("@/views/course"),
    }, //个人信息
    {
      path: "/information",
      name: "information",
      component: () => import("@/views/personalCenter/information"),
    },
    //错题集
    {
      path: "/wrongquestion",
      name: "wrongQuestion",
      component: () => import("@/views/personalCenter/wrongQuestion"),
    },
    //错题记录
    {
      path: "/wrongquestionrecord",
      name: "wrongQuestionRecord",
      component: () => import("@/views/personalCenter/wrongQuestionRecord"),
    },
    //错题记录-查看答案
    {
      path: "/wrongbanklo",
      name: "wrongBanklo",
      component: () => import("@/views/questionBank/wrongBanklo"),
    },
    //错题记录-错题练习
    {
      path: "/wrongbankex",
      name: "wrongBankex",
      component: () => import("@/views/questionBank/wrongBankex"),
    },
    //创新创业-产后护理
    {
      path: "/chIndex",
      name: "chIndex",
      component: () => import("@/views/cxcyCopyPage/chIndex"),
    },
    //创新创业-婴幼儿照护
    {
      path: "/yrIndex",
      name: "yrIndex",
      component: () => import("@/views/cxcyCopyPage/yrIndex"),
    },
    // 404
    {
      path: "*",
      name: "404",
      component: () => import("@/views/notfind"),
    },
  ],
});

// 不需要授权的地址url
const noAuthUrlList = [
  "/login",
  "/forgotpassword",
  "/",
  "/chIndex",
  "/yrIndex",
  "/login1",
];

// 页面回退禁止页面
const stopPage = [
  "/questionbank/continue",
  "/questionbank",
  "/questionbank/again",
];

router.beforeEach((to, from, next) => {
  // 重置滚动条
  window.scrollTo(0, 0);
  if (
    to.path == "/login" ||
    to.path == "/forgotpassword" ||
    to.path == "/login1"
  ) {
    store.dispatch("setHeadShow", false);
  } else {
    store.dispatch("setHeadShow", true);
  }
  // 顶部下标显示
  if (to.path == "/") {
    // 首页顶部样式
    store.dispatch("setActiveIndex", 0);
    store.dispatch("setBackColor", "#fff");
    store.dispatch("setFontColor", "#737980");
    store.dispatch("setOpacity", "0.8");
    store.dispatch("setHoColor", "");
    store.dispatch("setFilter", "blur(1px)");
  } else {
    // 除首页外的顶部样式
    store.dispatch("setBackColor", "#1968FA");
    store.dispatch("setFontColor", "#fff");
    store.dispatch("setOpacity", "1");
    store.dispatch("setHoColor", "#fff");
    store.dispatch("setFilter", "blur(0px)");
  }
  if (to.path == "/eventresources" || to.path == "/resourcesdetails") {
    store.dispatch("setActiveIndex", 2);
  }
  if (to.path == "/bookresources" || to.path == "/onexbook") {
    store.dispatch("setActiveIndex", 3);
  }
  if (to.path == "/skillsresources" || to.path == "/skills") {
    store.dispatch("setActiveIndex", 4);
  }
  if (to.path == "/courseresources" || to.path == "/course") {
    store.dispatch("setActiveIndex", 5);
  }
  if (to.path == "/industryresources" || to.path == "/industry") {
    store.dispatch("setActiveIndex", 6);
  }
  if (noAuthUrlList.includes(to.path)) {
    // 不需要授权的url直接放行
    next();
  } else {
    // 需要授权的做拦截判断
    if (!store.getters.token) {
      next("/login");
    } else {
      next();
    }
  }

  // 浏览器后退
  window.addEventListener("popstate", function () {
    if (store.state.userData.type == 0) {
      if (stopPage.includes(to.path)) {
        next("/studentrecord");
      } else {
        next("/");
      }
    }
    if (store.state.userData.type == 1) {
      if (stopPage.includes(to.path)) {
        next("/");
      }
    }
  });

  // 答题页跳转逻辑
  // if (
  //   to.path == "/questionbank/again" ||
  //   to.path == "/questionbank/continue" ||
  //   to.path == "/questionbank"
  // ) {
  //   if (from.path == "/questionbank/completed") {
  //     this.$router.push("/studentrecord");
  //   }
  // }
  // sessionStorage.setItem("goback", from.path);
  next();
});

export default router;
